package com.fb;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.widget.LinearLayout;
import com.fb.FileBrowserPage;
import com.dev.CheckableItem;
import com.dev.GUI;
import com.dev.GUI.Item;
import com.dev.IndentEditor;
import com.dev.MainActivity;
import com.dev.R;
import com.dev.FileBrowserPageHelper;

public class QuerySettings extends LinearLayout implements OnClickListener{
	WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
	Item itMore;
	CheckableItem itSortCreate1;
	CheckableItem itSortCreate2;
	CheckableItem ci1,ci2,ci3,ci4,ci5,ci6,ci7,ci8,ci9,cia,cib,cic,cid;
	MainActivity ma;
	int borderWidth=2;
	static QueryMoreSettings qms;
    public QuerySettings(MainActivity ma){
		super(ma);
		this.ma=ma;
		wlp.gravity=Gravity.LEFT|Gravity.TOP;
		wlp.flags=wlp.FLAG_LAYOUT_IN_SCREEN;
		setOrientation(1);
		setBackground(new GUI.BorderDrawable());
		itSortCreate1=new CheckableItem(ma,"按创建时间顺序排列");
		itSortCreate1.setOnClickListener(this);
		itSortCreate2=new CheckableItem(ma,"按创建时间逆序排列");
		itSortCreate2.setOnClickListener(this);
		itMore=new Item(ma);
		itMore.setOnClickListener(this);
		itMore.setText("更多");
		ci1=new CheckableItem(ma,"自动折行");
		ci2=new CheckableItem(ma,"使用宽视口");
		ci3=new CheckableItem(ma,"JavaScript");
		ci4=new CheckableItem(ma,"目录在前");
		ci5=new CheckableItem(ma,"文件在前");
		ci6=new CheckableItem(ma,"按名称顺序排列");
		ci7=new CheckableItem(ma,"按名称逆序排列");
		ci8=new CheckableItem(ma,"缩进视图");
		ci9=new CheckableItem(ma,"按文件大小顺序排列");
		cia=new CheckableItem(ma,"按文件大小逆序排列");
		cib=new CheckableItem(ma,"按最后修改时间顺序排列");
		cic=new CheckableItem(ma,"按最后修改时间逆序排列");
		cid=new CheckableItem(ma,"显示隐藏文件");
		ci1.setOnClickListener(this);
		ci2.setOnClickListener(this);
		ci3.setOnClickListener(this);
		ci4.setOnClickListener(this);
		ci5.setOnClickListener(this);
		ci6.setOnClickListener(this);
		ci7.setOnClickListener(this);
		ci8.setOnClickListener(this);
		ci9.setOnClickListener(this);
		cia.setOnClickListener(this);
		cib.setOnClickListener(this);
		cic.setOnClickListener(this);
		cid.setOnClickListener(this);
		ci1.borderWidth=borderWidth;
		ci2.borderWidth=borderWidth;
		ci3.borderWidth=borderWidth;
		ci4.borderWidth=borderWidth;
		ci5.borderWidth=borderWidth;
		ci6.borderWidth=borderWidth;
		ci7.borderWidth=borderWidth;
		ci8.borderWidth=borderWidth;
		ci9.borderWidth=borderWidth;
		cia.borderWidth=borderWidth;
		cib.borderWidth=borderWidth;
		cic.borderWidth=borderWidth;
		cid.borderWidth=borderWidth;
		LinearLayout.LayoutParams itlp=new LinearLayout.LayoutParams(-1,ma.dialogTitleBarHeight);
		addView(ci1,itlp);
		addView(ci2,itlp);
		addView(ci3,itlp);
		addView(ci4,itlp);
		addView(ci5,itlp);
		addView(ci6,itlp);
		addView(ci7,itlp);
		addView(ci9,itlp);
		addView(cia,itlp);
		addView(cib,itlp);
		addView(cic,itlp);
		addView(itSortCreate1,itlp);
		addView(itSortCreate2,itlp);
		addView(cid,itlp);
		addView(ci8,itlp);
		addView(itMore,itlp);
	}
	FileBrowserPage fbp;
	public void show(FileBrowserPage fbp){
		this.fbp=fbp;
		wlp.width=ma.wp/2;
		wlp.height=-2;
		Rect rect=new Rect();
		fbp.ivSetting.getGlobalVisibleRect(rect);
		wlp.x=rect.left;
		wlp.y=rect.bottom;
		cid.setSelected(fbp.showHidden);
		ci8.setSelected(fbp.indentItem);
		if(fbp.sort==0){
			ci6.setSelected(false);
			ci7.setSelected(false);
			ci9.setSelected(false);
			cia.setSelected(false);
			cib.setSelected(false);
			cic.setSelected(false);
		}else if(fbp.sort==1){
			ci6.setSelected(true);
			ci7.setSelected(false);
			ci9.setSelected(false);
			cia.setSelected(false);
			cib.setSelected(false);
			cic.setSelected(false);
		}else if(fbp.sort==2){
			ci6.setSelected(false);
			ci7.setSelected(true);
			ci9.setSelected(false);
			cia.setSelected(false);
			cib.setSelected(false);
			cic.setSelected(false);
		}else if(fbp.sort==3){
			ci6.setSelected(false);
			ci7.setSelected(false);
			ci9.setSelected(true);
			cia.setSelected(false);
			cib.setSelected(false);
			cic.setSelected(false);
		}else if(fbp.sort==4){
			ci6.setSelected(false);
			ci7.setSelected(false);
			ci9.setSelected(false);
			cia.setSelected(true);
			cib.setSelected(false);
			cic.setSelected(false);
		}else if(fbp.sort==5){
			ci6.setSelected(false);
			ci7.setSelected(false);
			ci9.setSelected(false);
			cia.setSelected(false);
			cib.setSelected(true);
			cic.setSelected(false);
		}else if(fbp.sort==6){
			ci6.setSelected(false);
			ci7.setSelected(false);
			ci9.setSelected(false);
			cia.setSelected(false);
			cib.setSelected(false);
			cic.setSelected(true);
		}
		ma.wm.addView(this,wlp);
	}
	void dismiss(){
		ma.wm.removeView(this);
	}
	@Override
	public void onClick(View view) {
		if(view==itSortCreate1){
			if(itSortCreate1.isSelected()){
				itSortCreate1.setSelected(false);
			}else{
				itSortCreate2.setSelected(true);
			}
		}else if(view==itSortCreate2){
			if(itSortCreate2.isSelected()){
				itSortCreate2.setSelected(false);
			}else{
				itSortCreate2.setSelected(true);
			}
		}else if(view==itMore){
			if(qms==null)qms=new QueryMoreSettings(ma);
			qms.show();
		}else if(view==ci1){//onClick
			IndentEditor ie=fbp.ie;
			if(ci1.box.isSelected()){
				ci1.box.setSelected(false);
				if(ie.getVisibility()==View.VISIBLE){
					ie.letHorizontalScrollable(true);
					ie.invalidate();
				}
			}else if(ie.getVisibility()==View.VISIBLE){
				ci1.box.setSelected(true);
				ie.letHorizontalScrollable(false);
				ie.invalidate();
			}
		}else if(view==ci2){//onClick
			WebSettings ws=fbp.hb.webSettings;
			if(ci2.box.isSelected()){
				ci2.box.setSelected(false);
				ws.setUseWideViewPort(false);
			}else{
				ci2.box.setSelected(true);
				ws.setUseWideViewPort(true);
			}
		}else if(view==ci3){//onClick
			WebSettings ws=fbp.hb.webSettings;
			if(ci3.box.isSelected()){
				ci3.box.setSelected(false);
				ws.setJavaScriptEnabled(false);
			}else{
				ci3.box.setSelected(true);
				ws.setJavaScriptEnabled(true);
			}
		}else if(view==ci4){//onClick,文件夹在前
			if(ci4.box.isSelected()){
				ci4.box.setSelected(false);
				fbp.folderOnBefore=false;
				FileBrowserPageHelper.makeSort(fbp);
			}else{
				ci4.box.setSelected(true);
				ci5.box.setSelected(false);
				fbp.folderOnBefore=true;
				fbp.fileOnBefore=false;
				FileBrowserPageHelper.makeSort(fbp);
			}
		}else if(view==ci5){//onClick,文件在前
			if(ci5.box.isSelected()){
				ci5.box.setSelected(false);
				fbp.fileOnBefore=false;
				FileBrowserPageHelper.makeSort(fbp);
			}else{
				ci5.box.setSelected(true);
				ci4.box.setSelected(false);
				fbp.fileOnBefore=true;
				fbp.folderOnBefore=false;
				FileBrowserPageHelper.makeSort(fbp);
			}
		}else if(view==ci6){//onClick,sort by name
			if(ci6.isSelected()){
				ci6.setSelected(false);
				fbp.sort=0;
				FileBrowserPageHelper.makeSort(fbp);
			}else{
				ci6.setSelected(true);
				ci7.setSelected(false);
				ci9.setSelected(false);
				cia.setSelected(false);
				cib.setSelected(false);
				cic.setSelected(false);
				fbp.sort=1;
				FileBrowserPageHelper.makeSort(fbp);
			}
		}else if(view==ci7){//onClick
			if(ci7.isSelected()){
				ci7.setSelected(false);
				fbp.sort=0;
				FileBrowserPageHelper.makeSort(fbp);
			}else{
				ci6.setSelected(false);
				ci7.setSelected(true);
				ci9.setSelected(false);
				cia.setSelected(false);
				cib.setSelected(false);
				cic.setSelected(false);
				fbp.sort=2;
				FileBrowserPageHelper.makeSort(fbp);
			}
		}else if(view==ci8){//onClick
			if(ci8.isSelected()){
				ci8.setSelected(false);
				fbp.indentItem=false;
				fbp.ivMeans.setImageResource(R.drawable.finger_south);
				fbp.dlv.invalidate();
			}else{
				ci8.setSelected(true);
				fbp.indentItem=true;
				fbp.ivMeans.setImageResource(R.drawable.ic_arrow_compass);
				fbp.dlv.invalidate();
			}
		}else if(view==ci9){//sort by file size
			if(ci9.isSelected()){
				ci9.setSelected(false);
				fbp.sort=0;
				FileBrowserPageHelper.makeSort(fbp);
			}else{
				ci6.setSelected(false);
				ci7.setSelected(false);
				ci9.setSelected(true);
				cia.setSelected(false);
				cib.setSelected(false);
				cic.setSelected(false);
				fbp.sort=3;
				FileBrowserPageHelper.makeSort(fbp);
			}
		}else if(view==cia){
			if(cia.isSelected()){
				cia.setSelected(false);
				fbp.sort=0;
				FileBrowserPageHelper.makeSort(fbp);
			}else{
				ci6.setSelected(false);
				ci7.setSelected(false);
				ci9.setSelected(false);
				cia.setSelected(true);
				cib.setSelected(false);
				cic.setSelected(false);
				fbp.sort=4;
				FileBrowserPageHelper.makeSort(fbp);
			}
		}else if(view==cib){
			if(cib.isSelected()){
				cib.setSelected(false);
				fbp.sort=0;
				FileBrowserPageHelper.makeSort(fbp);
			}else{
				ci6.setSelected(false);
				ci7.setSelected(false);
				ci9.setSelected(false);
				cia.setSelected(false);
				cib.setSelected(true);
				cic.setSelected(false);
				fbp.sort=5;
				FileBrowserPageHelper.makeSort(fbp);
			}
		}else if(view==cic){
			if(cic.isSelected()){
				cic.setSelected(false);
				fbp.sort=0;
				FileBrowserPageHelper.makeSort(fbp);
			}else{
				ci6.setSelected(false);
				ci7.setSelected(false);
				ci9.setSelected(false);
				cia.setSelected(false);
				cib.setSelected(false);
				cic.setSelected(true);
				fbp.sort=6;
				FileBrowserPageHelper.makeSort(fbp);
			}
		}else if(view==cid){
			if(cid.isSelected()){
				cid.setSelected(false);
				fbp.showHidden=false;
				FileBrowserPageHelper.makeSort(fbp);
			}else{
				cid.setSelected(true);
				fbp.showHidden=true;
				FileBrowserPageHelper.makeSort(fbp);
			}
		}
	}
	Rect visibile=new Rect();
	@Override
	protected void onLayout(boolean changed,int l,int t,int r,int b){
		super.onLayout(changed,l,t,r,b);
		getGlobalVisibleRect(visibile);

	}
	@Override
	public boolean onTouchEvent(MotionEvent event){
		int rx=(int)event.getX();
		int ry=(int)event.getY();
		switch(event.getAction()){
			case MotionEvent.ACTION_DOWN:
				if(rx<visibile.left||rx>=visibile.right||ry<visibile.top||ry>=visibile.bottom){
					dismiss();
				}
		}
		return super.onTouchEvent(event);
	}
}
